import org.zkoss.zul.api.Audio;
import org.zkoss.zul.api.Button;
import org.zkoss.zul.api.Image;
import org.zkoss.zul.api.Timer;
import org.zkoss.sound.AAudio;
import org.zkoss.zul.Window;
import java.net.URL;

import validation.VCodeProcessor;

public class VCodeGeneratorController extends Window
{
	private boolean useVoice=false;
	private int imgState=1;
	private Button modeChange,refresh;
	private Image codeImg;
	private Audio voicePlayer;
	private long time=0;
	private String notFoundImg,
	               refreshImg="VCodeGenerator/refresh_icon.png",
	               useVoiceImg="VCodeGenerator/sound_icon.png",
	               useCodeImg="VCodeGenerator/code_icon.png",
	               loadingImg="VCodeGenerator/loading_icon.gif",
	               playingImg="VCodeGenerator/playVoice.png";
	private String relURL;
	
	public void initialize(){
		modeChange=((Button)getFellow("VG_USE_VOICE"));
		refresh=((Button)getFellow("VG_REFRESH"));
		codeImg=((Image)getFellow("VG_IMG"));
		voicePlayer=((Audio)getFellow("VG_VOICE_PLAYER"));
		
		modeChange.setImage(useVoiceImg);
		refresh.setImage(refreshImg);
		codeImg.setSrc(loadingImg);
		voicePlayer.setVisible(false);
		this.addEventListener("onNotify",new VCodeProcessor());
		
		codeImg.setAttribute("found", "false");
		setAttribute("EVENT_TYPE","REQUEST_NEW_CODE");
		Events.echoEvent("onNotify",this,"");
		time=System.currentTimeMillis();
		/*
		relURL=this.getAttribute("relURL").toString();
		if(relURL == null)return;
		if(notFoundImg != null)notFoundImg=notFoundImg.substring(0,3)+relURL+notFoundImg.substring(3);
		if(refreshImg != null)refreshImg=refreshImg.substring(0,3)+relURL+refreshImg.substring(3);
		if(useVoiceImg != null)useVoiceImg=useVoiceImg.substring(0,3)+relURL+useVoiceImg.substring(3);
		if(useCodeImg != null)useCodeImg=useCodeImg.substring(0,3)+relURL+useCodeImg.substring(3);
		if(loadingImg != null)loadingImg=loadingImg.substring(0,3)+relURL+loadingImg.substring(3);
		if(playingImg != null)playingImg=playingImg.substring(0,3)+relURL+playingImg.substring(3);*/
	}
	
	public void changeState(){
		if(System.currentTimeMillis()-time < 2000)
			return;
		
		useVoice=!useVoice;
		codeImg.setSrc(loadingImg);
		if(useVoice){
			modeChange.setImage(useCodeImg);
			codeImg.setAttribute("found", "false");
			setAttribute("EVENT_TYPE","REQUEST_NEW_VOICE");
			codeImg.setAttribute("src", playingImg);
			Events.echoEvent("onNotify",this,"");
			time=System.currentTimeMillis();
		}else{
			modeChange.setImage(useVoiceImg);
			codeImg.setAttribute("found", "false");
			setAttribute("EVENT_TYPE","REQUEST_NEW_CODE");
			Events.echoEvent("onNotify",this,"");
			time=System.currentTimeMillis();
		}
	}
	
	public void refreshCode(){
		if(System.currentTimeMillis()-time < 2000)
			return;
		
		codeImg.setSrc(loadingImg);
		codeImg.setAttribute("found", "false");
		setAttribute("EVENT_TYPE",(useVoice ? "REQUEST_NEW_VOICE" : "REQUEST_NEW_CODE"));
		Events.echoEvent("onNotify",this,"");
		time=System.currentTimeMillis();
	}
		
	public void play(){
		if(!useVoice || codeImg.getAttribute("found") == null || codeImg.getAttribute("found").equals("false"))
			return;
		else voicePlayer.play();
	}
	
	public String getCode(){
		return codeImg.getAttribute("code").toString();
	}
}